package com.wyz.saas.app.mvvm.account

import android.content.Context
import android.content.Intent
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.graphics.drawable.DrawableCompat
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.wyz.saas.app.R
import com.wyz.saas.app.databinding.ActivityAccountBinding
import com.wyz.saas.app.mvvm.AppViewModelFactory
import com.wyz.saas.common.base.AbstractApiContainerActivity
import net.qiujuer.genius.ui.compat.UiCompat
import timber.log.Timber

class AccountActivity : AbstractApiContainerActivity<ActivityAccountBinding, AccountViewModel>() {

    companion object {
        fun show(context: Context) {
            context.startActivity(Intent(context, AccountActivity::class.java))
        }
    }

    override fun getFrameLayoutId(): Int = R.id.lay_container

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.activity_account

    override fun initViewModel(): AccountViewModel =
        ViewModelProvider(this, AppViewModelFactory).get(AccountViewModel::class.java)

    override fun initData(view: View?) {
        super.initData(view)
        Timber.e("%s -> Viewmodel %d", this.javaClass.name, viewModel?.hashCode())
        loadFragment(LoginFragment(), false)
        // 初始化背景 用来学习
        Glide.with(this)
            .load(R.mipmap.bg_src_tianjin)
            .centerCrop() //居中剪切
            .into(object : SimpleTarget<Drawable>() {
                override fun onResourceReady(
                    resource: Drawable,
                    transition: Transition<in Drawable>?
                ) {
                    // 拿到glide的Drawable
                    var drawable: Drawable = resource.current
                    // 使用适配类进行包装
                    drawable = DrawableCompat.wrap(drawable)
                    drawable.setColorFilter(
                        UiCompat.getColor(resources, R.color.colorAccent),
                        PorterDuff.Mode.SCREEN
                    )
                    // 设置着色的效果和颜色，蒙板模式
                    // 设置给ImageView
                    view?.findViewById<ImageView>(R.id.im_bg)?.setImageDrawable(drawable)
                }
            })
    }
}